package fr.mairie.parc.dao.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import fr.mairie.parc.dao.InstallationDao;
import fr.mairie.parc.dao.mapper.InstallationRowMapper;
import fr.mairie.parc.domain.Installation;

public class InstallationDaoImpl implements InstallationDao{
	
	
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbcTempalete() {
		return jdbcTemplate;
	}

	@Autowired
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Installation> selectAll() {
		return jdbcTemplate.query("SELECT idAttribution, idAttribution, idTechnicien, ficheInstallation, etat, date" +
				"" + " FROM installation",
						new InstallationRowMapper());
	}

	@Override
	public void addInstallation(Installation installation) {
		jdbcTemplate.update("INSERT INTO installation (idAttribution, idTechnicien, ficheInstallation, etat, date) values (?,?,?,?,?)",
				new Object [] {
							   installation.getIdAttribution(),
							   installation.getIdTechnicien(),
							   installation.getFicheInstallation(),
							   installation.getEtat(),
							   installation.getDate()
							   
							   });
		
	}

	@Override
	public Installation getInstallationById(int id) {
		return (Installation) jdbcTemplate.queryForObject("SELECT * FROM installation WHERE idInstallation=?",
				new Object [] {id},
				new InstallationRowMapper());
	}

	@Override
	public void updateInstallation(Installation installation) {
		jdbcTemplate.update("UPDATE installation SET etat = ? AND date=? AND " +
				" WHERE idInstallation = ? ",
                new Object [] {installation.getEtat(),
							   installation.getDate()
		
				});
	}

	@Override
	public void deleteInstallation(Installation installation) {
		jdbcTemplate.update("DELETE FROM installation WHERE idInstallation= ?", 
				new Object[] {installation.getIdInstallation()});
		
	}

}
